#include "stdafx.h"
#include "ui.h"
#include "texture.h"
#include "ScriptSystem.h"

#include "DXUT.h"
#include "DXUTgui.h"
#include "DXUTguiIME.h"

UIGadget::UIGadget()
{
	Reset();
	dlg = new CDXUTDialog();
}

UIGadget::~UIGadget()
{
	SAFE_DELETE(dlg);
}

void UIGadget::AddControl( Control &ctrl )
{
	if( !dlg )
		return;
	TCHAR wtext[NAME_LEN];
	::MultiByteToWideChar( CP_ACP, MB_PRECOMPOSED, ctrl.text, -1, wtext, NAME_LEN );
	switch (ctrl.type)
	{
	case eCtrl_Button:
		dlg->AddButton( ctrl.id, wtext, ctrl.x, ctrl.y, ctrl.width, ctrl.height );
		break;
	case eCtrl_Static:
        dlg->AddStatic( ctrl.id, wtext, ctrl.x, ctrl.y, ctrl.width, ctrl.height );
		break;
	default:
		break;
	}
    SetControlTexture( ctrl.id, L"data\\UI\\Textures\\Button.png" );
}

void UIGadget::SetPosition( int x, int y )
{
	dlg->SetLocation(x, y);	
}

void UIGadget::SetSize( int width, int height )
{
	dlg->SetSize( width, height );
}

void UIGadget::SetBackground( LPWCH filename )
{
    dlg->EnableBackground( true );
    dlg->SetBackgroundTexture( filename );
}

void UIGadget::SetVisible( bool visible )
{
    dlg->SetVisible( visible );
}

void UIGadget::SetControlTexture( int controlid,LPWCH filename )
{
    CDXUTControl *ctrl = dlg->GetControl( controlid );
    CHECK_PTR_VOID(ctrl);
    int texid = ctrl->m_Index + 1;
    dlg->SetTexture( texid, filename );
    DXUTTextureNode *node = dlg->GetTexture( texid );
    CHECK_PTR_VOID( node );
    D3DSURFACE_DESC desc;
    node->pTexture9->GetLevelDesc( 0, &desc );
    RECT rctexture = { 0, 0, desc.Width, desc.Height };
    for( int i=0; i<ctrl->m_Elements.GetSize(); i++ )
    {
        CDXUTElement *elem = ctrl->GetElement( i );
        elem->SetTexture( texid, &rctexture );
    } 
}

void UIGadget::SetControlFont( int controlid, LPCWSTR strFaceName, int height, int weight, int color )
{
    CDXUTControl *ctrl = dlg->GetControl( controlid );
    CHECK_PTR_VOID(ctrl);
    int fontid = ctrl->m_Index + 1;
    dlg->SetFont( fontid, strFaceName, height, weight );
    for( int i=0; i<ctrl->m_Elements.GetSize(); i++ )
    {
        CDXUTElement *elem = ctrl->GetElement( i );
        elem->SetFont( fontid, color );
    } 
}

UI::UI()
{
	m_pDlgManager = NULL;
}

UI::~UI()
{
	Clear();
}

void UI::Init()
{
	m_pDlgManager = new CDXUTDialogResourceManager();
	HRESULT h = m_pDlgManager->OnD3D9CreateDevice( CD3DManager::Instance().GetDevice() );
	ASSERT( h == S_OK );
	h = m_pDlgManager->OnD3D9ResetDevice();
	ASSERT( h == S_OK );
}

void UI::Update( int diff )
{
	Script.CallLuaFunc<void>( "Update", diff );
}

void UI::Render( int diff )
{
	for( uint32 i=0; i<m_dlgqueue.size(); i++ )
	{
		RenderUI(m_dlgqueue[i], (float)diff);
	}
}

void UI::RenderUI( int id, float diff )
{
	UIGadget *ui = Find( id );
	if( ui )
		ui->dlg->OnRender( diff );
}

void UI::Clear()
{
	for( uint32 i=0; i<m_dlgqueue.size(); i++ )
	{
		UIGadget *ui = Find( m_dlgqueue[i] );
		SAFE_DELETE(ui);
	}
	if(m_pDlgManager)
		m_pDlgManager->OnD3D9DestroyDevice();
	SAFE_DELETE(m_pDlgManager);
}

UIGadget * UI::CreateGadget( int id )
{
	UIGadget *gadget = new UIGadget();
	gadget->dlgID = id;
	gadget->dlg->Init( m_pDlgManager );
	gadget->dlg->SetCallback( UI::HandleEvent, gadget );
	Insert( id, gadget );
	m_dlgqueue.push_back(id);
	return gadget;
}

bool UI::MsgProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
{
	bool stop = m_pDlgManager->MsgProc( hWnd, uMsg, wParam, lParam );
	if( stop )
		return true;
	for( uint32 i=0; i<m_dlgqueue.size(); i++ )
	{
		UIGadget *ui = Find( m_dlgqueue[i] );
		stop = ui->dlg->MsgProc( hWnd, uMsg, wParam, lParam );
		if( stop )
			return true;
	}
	return false;
}

void __stdcall UI::HandleEvent( UINT nEvent, int nControlID, CDXUTControl* pControl, void* pUserContext )
{
	if( pUserContext == NULL )
		return;
	UIGadget *gadget = (UIGadget *)pUserContext;
	Script.CallLuaFunc<void>("HandleEvent", gadget->dlgID, nEvent, nControlID, pControl);
}